package com.itheima.sh.e_test;

import com.itheima.sh.e_test.Car;

import java.util.ArrayList;


public class CarTest {
    //定义方法
    public static ArrayList<Car> filter(ArrayList<Car> list, int price, int mile) {
        //创建一个新的集合
        ArrayList<Car> c = new ArrayList<>();
        //遍历list集合，获取信息
        for (int i = 0; i < list.size(); i++) {
            Car car = list.get(i);
            String brand = car.getBrand();
            int price1 = car.getPrice();
            int mile1 = car.getMile();
            //判断价格和续航公里
            if (price1 < price && mile1 >= mile) {
                c.add(car);
            }
        }
        //返回新的集合
        return c;
    }

    public static void main(String[] args) {
        //创建 3 个 car 对象
        Car car1 = new Car("凯迪拉克 XTS", 179800, 400);
        Car car2 = new Car("大众 BS", 448000, 500);
        Car car3 = new Car("宝马 X5", 828000, 552);
       //将以上3个对象放入list_car集合中
        ArrayList<Car> list_car = new ArrayList<>();
        list_car.add(car1);
        list_car.add(car2);
        list_car.add(car3);
        //调用filter 方法传入 list_car,500000 和 400
        ArrayList<Car> filter = filter(list_car, 500000, 400);
        for (int i = 0; i < filter.size(); i++) {
            Car car = filter.get(i);
            String brand = car.getBrand();
            int price = car.getPrice();
            int mile = car.getMile();
            //输出结果
            System.out.println(brand + "\t" + price + "\t" + mile);
        }
    }
}
